package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author guozh
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    public SetmealService setmealService;

    /**
     * 增加套餐
     *
     * @param setmealDTO 套餐数据
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @PostMapping()
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐数据{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO 套餐分页查询数据
     * @return Result<PageResult>
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除
     *
     * @param ids id
     * @return Result<String>
     */
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("批量删除id{}", ids);
        setmealService.deleteById(ids);
        return Result.success();
    }

    /**
     * 根据id查找
     *
     * @param id id
     * @return Result<SetmealVO>
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> findById(@PathVariable Long id) {
        log.info("根据id查找{}", id);
        SetmealVO setmealVO = setmealService.findById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO 修改套餐数据
     * @return Result<String>
     */
    @PutMapping()
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐数据{}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 修改状态
     * @param status 状态数据
     * @return Result<String>
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, Long id){
        log.info("修改状态数据{} id{}",status,id);
        setmealService.updateStatus(status,id);
        return Result.success();
    }
}
